import axios from 'axios'
import Notify from '@/components/prompt/prompt.js'

const request = axios.create({
  baseURL: process.env.VUE_APP_SERVE,
  timeout: 10 * 5000
})
// 响应拦截器
request.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  token && (config.headers.Authorization = token)
  return config
}, error => {
  console.log('请求拦截器 - 发生错误:', error)
  return Promise.reject(error)
})
// 响应拦截器
request.interceptors.response.use(response => {
  let { code, msg } = response.data
  switch (code) {
    case 200:
      return response.data
    case 401:
      Notify({
        type: 'danger',
        message: msg
      })
      break;
    case 403:
      Notify({
        type: 'danger',
        message: msg
      })
      break;
    case 404:
      Notify({
        type: 'danger',
        message: msg
      })
      break;
  }
}, error => {
  return Promise.reject(error);
});
export default request